import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:proj_sfv/component/MyNotification.dart';
import 'package:proj_sfv/domain/Comment.dart';
import 'package:proj_sfv/domain/Material.dart';
import 'package:proj_sfv/domain/Profile.dart';
import 'package:proj_sfv/domain/UserDetail.dart';
import 'package:proj_sfv/domain/UserInfo.dart';
import 'package:proj_sfv/domain/Video.dart';
import 'package:proj_sfv/domain/VideoDTO.dart';
import 'package:proj_sfv/domain/videoVo.dart';
import 'package:proj_sfv/http/api.dart';
import 'package:proj_sfv/main.dart';
import 'package:proj_sfv/pages/myPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

import '../domain/UserVo.dart';

const jsonFormat = <String, String>{
  'Content-Type': 'application/json; charset=UTF-8',
};


Map<String, dynamic> convertString2Json(String body) {
  var str = utf8.decoder.convert(body.codeUnits);
  return jsonDecode(str);
}

bool handleFailure(BuildContext context, http.Response response) {

  if (response.statusCode == 200) {
    return true;
  } else if (response.statusCode == 500) {
    showDialog(context: context, builder: (BuildContext build) =>
      MyNotification("服务器错误", oneConfirmButton(build))
    );
  } else {
    var str = utf8.decoder.convert(response.bodyBytes);
    var data = jsonDecode(str);
    if (response.statusCode == 401 || response.statusCode == 400) {
      showDialog(context: context, builder: (BuildContext build) =>
          MyNotification(data['message'], [
            TextButton(
                onPressed: () => Navigator.pop(context), child: Text("确认"))
          ]));
    } else if (response.statusCode == 406) {
      showDialog(context: context, builder: (BuildContext build) =>
          MyNotification(data['message'], [
            TextButton(
                onPressed: () {
                  SharedPreferences.getInstance().then((value)
                  {
                    value.remove("id");
                    value.remove("token");
                  });
                  TIMUIKitCore.getInstance().logout();
                  Navigator.popUntil(context, (route) => route.isFirst);
                }, child: Text("确认"))
          ]));
    }
  }
    return false;
  }



Future<Map<String, dynamic>> loginRequest(BuildContext context, String username, String password) async {
  Uri url = Uri.http(gatewayIp, login);
  var resp = await http.post(url,
      headers: jsonFormat,
      body: jsonEncode(<String, String>{
        "username": username,
        "password": password
      }), encoding: Encoding.getByName("utf-8")
  );
  return handleFailure(context, resp) ? jsonDecode(resp.body)['data'] : null;
}

void logoutRequest(BuildContext context, int id, String token) {
  Uri url = Uri.http(gatewayIp, logout + id.toString());
  http.get(url, headers: {
    "token": token
  });
}

Future<Map<String, dynamic>> registerRequest(BuildContext context, String profile, String username,
  String email, String password, bool gender) async {
  Uri url = Uri.http(gatewayIp, register);
  var resp = await http.post(url, headers: jsonFormat,
    body: jsonEncode({
      "userName": username,
      "email": email,
      "password": password,
      "gender": gender,
      "profile": profile
    })
  );
  var str = utf8.decoder.convert(resp.body.codeUnits);
  return handleFailure(context, resp) ? jsonDecode(str)['data'] : null;
}

Future<UserInfo> getBasicMyRequest(BuildContext context, int id, String token) async {
  Uri url = Uri.http(gatewayIp, getUserMy + id.toString());
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body);
    return UserInfo.fromJson(data['data']);
  } else {
    return null;
  }
}

Future<String> updateUserInfo(BuildContext context, int id, String token, String filedName, String value) async {
  var url = Uri.http(gatewayIp, '$setUserInfo$id');
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.put(url, headers: header
  , body: jsonEncode(<String, String>{
    "fieldName": filedName,
    "value": value
  }), encoding: Encoding.getByName("utf-8"));
  if (handleFailure(context, resp)) {
    return "update success";
  } else {
    return null;
  }
}

Future<List<Profile>> getHistoryProfileRequest(BuildContext context, int id, String token) async {
  Uri url = Uri.http(gatewayIp, '$getHistoryProfiles$id');
  var resp = await http.get(url,
  headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    List<Profile> result = [];
    var data = convertString2Json(resp.body)['data'];
    print('data $data');
    for (var item in data) {
      print('item $item');
      result.add(Profile.fromJson(item));
    }
    return result;
  } else {
    return null;
  }

}

Future<String> updateProfileRequest(BuildContext context, int id, String token, String base64) async {
  Uri url = Uri.http(gatewayIp, '$setCurrentProfile$id');
  var resp = await http.put(url, headers: {
    "token": token
  }, body: base64);
  if (handleFailure(context, resp)) {
    return jsonDecode(resp.body)['data'];
  } else {
    return null;
  }
}

Future<String> updateProfileHistoricalRequest(BuildContext context, int id, String token, String profileId) async {
  Uri url = Uri.http(gatewayIp, '$setCurrentProfileHistorical$id');
  var resp = await http.put(url, headers: {
    "token": token
  }, body: profileId);
  if (handleFailure(context, resp)) {
    return jsonDecode(resp.body)['message'];
  } else {
    return null;
  }
}

Future<List<UserVo>> searchUserListRequest(BuildContext context, String text, String token, {int page = 1}) async {
  Uri url = Uri.http(gatewayIp, '$getUserListBySearch$text', {
    "page": '$page'
  });
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body)['data'];
    List<UserVo> result = [];
    for (var item in data) {
      print('item: $item');
      result.add(UserVo.fromJson(item));
    }
    print('result $result');
    return result;
  } else {
    return null;
  }
}

enum FRIEND_RELATION_MEG {
  REQ, ACP, REJ, DEL }
extension FRIEND_RELATION_EXTETION  on FRIEND_RELATION_MEG {
  String get name {
    switch(this) {
      case FRIEND_RELATION_MEG.REQ:
        return "request";
      case FRIEND_RELATION_MEG.ACP:
        return "accept";
      case FRIEND_RELATION_MEG.REJ:
        return "reject";
      case FRIEND_RELATION_MEG.DEL:
        return "delete";
    }
  }
}

Future<String> addFriendRequest(BuildContext context, int id, String token,
    int otherId, {FRIEND_RELATION_MEG meg = FRIEND_RELATION_MEG.REQ}) async {
  Uri url = Uri.http(gatewayIp, '$addFriend$id');
  var header = <String, String> {
    "token": token
  };
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers: header,
    body: jsonEncode(<String, dynamic>{
      "id": otherId,
      "message": meg.name
    }));
  if (handleFailure(context, resp)) {
    return "发送成功";
  } else {
    return null;
  }
}

Future<UserDetail> getUserDetailInfoRequest(BuildContext context, String token, int otherId) async {
  Uri url = Uri.http(gatewayIp, getUserBasicInfo + otherId.toString());
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var json = convertString2Json(resp.body)['data'];
    print('json $json');
    var url = Uri.https(cosHost, '${json['profile']}.jpg').toString();
    return UserDetail.fromJson(json);
  } else {
    return null;
  }
}

Future<List<dynamic>> getUserRelationRequest(BuildContext context, int id, String token, int otherId) async {
  // 三种关系 好友 关注 拉黑
  Uri url = Uri.http(gatewayIp, '$getUserRelation$id/$otherId');
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body)['data'];
    print('data type : ${data[0].runtimeType}');
    return data;
  } else {
    return null;
  }
}

Future<String> followUserRequest(BuildContext context, int id, String token, int otherId) async {
  Uri url = Uri.http(gatewayIp, '$followUser$id');
  var header = <String, String>{
    "token": token
  };
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers: header, body: jsonEncode(<String, String>{
    "id": otherId.toString()
  }));
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

Future<String> unfollowUserRequest(BuildContext context, int id, String token, int otherId) async {
  Uri url = Uri.http(gatewayIp, '$followUser$id');
  var header = <String, String>{
    "token": token
  };
  header.addEntries(jsonFormat.entries);
  var resp = await http.delete(url, headers: header, body: jsonEncode(<String, String>{
    "id": otherId.toString()
  }));
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

enum BlockedMeg {
  REQUEST,
  FREE
}

extension BlockedMegExtension on BlockedMeg {
  String get data {
    switch(this) {
      case BlockedMeg.REQUEST:
        return "req";
      case BlockedMeg.FREE:
        return "free";
    }
  }
}

Future<String> blockUserRequest(BuildContext context, int id, String token, int otherId, BlockedMeg meg) async {
  Uri url = Uri.http(gatewayIp, '$setBlocked$id');
  var header = <String, String>{
    "token": token
  };
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers: header, body: jsonEncode(<String, String>{
    "id": otherId.toString(),
    "message": meg.data
  }));
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

Future<List<UserVo>> getUserVoListByUrlRequest(BuildContext context, String token, String url, {int page = 1}) async {
  Uri requestUrl = Uri.http(gatewayIp, url, {
    "page": page.toString()
  });
  print('request Url : $requestUrl');
  var resp = await http.get(requestUrl, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    List<UserVo> result = [];
    List<dynamic> list = convertString2Json(resp.body)['data'];
    for (var item in list) {
      result.add(UserVo.fromJson(item));
    }
    return result;
  } else {
    return null;
  }
}

Future<Map<String, dynamic>> uploadVideoForSigRequest(BuildContext context, String token, VideoDTO videoDTO) async {
  Uri url = Uri.http(gatewayIp, addVideo);
  var header = {
    "token": token
  };
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers: header,
    body: videoDTO.toJson()
  );
  if (handleFailure(context, resp)) {
    var json = convertString2Json(resp.body)['data'];
    return json;
  } else {
    return null;
  }
}

// 获取所有的素材
Future<List<MyMaterial>> getAllMaterialsRequest(BuildContext context) async {
  Uri url = Uri.http(gatewayIp, getMaterial);
  var resp = await http.get(url);
  if (handleFailure(context, resp)) {
    var list  = convertString2Json(resp.body)['data'] as List<dynamic>;
    List<MyMaterial> result = [];
    list.forEach((element) {
      result.add(MyMaterial.fromJson(element));
    });
    return result;
  } else {
    return null;
  }
}

// 获得我的所有视频
Future<List<VideoVo>> getMyVideosRequest(BuildContext context, String token, int id, {int page = 1}) async {
  Uri url = Uri.http(gatewayIp, '$getMyVideo$id', {"page": '$page'});
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var list = convertString2Json(resp.body)['data'] as List<dynamic>;
    List<VideoVo> result = [];
    for (var item in list) {
      result.add(VideoVo.fromJson(item));
    }
    return result;
  } else {
    return null;
  }
}

enum VideoPreference {
  LIKE,
  DISLIKE
}
// 获得我的所有喜欢的视频
Future<List<VideoVo>> getLikeVideosRequest(BuildContext context, int id, String token, VideoPreference type, {int page = 1}) async {
  String unEncodeUrl = "";
  switch(type) {
    case VideoPreference.LIKE:
      unEncodeUrl = '$getMyLikeVideo$id/$page';
      break;
    case VideoPreference.DISLIKE:
      unEncodeUrl = '$getMyDiskLikeVideo$id/$page';
  }
  Uri url = Uri.http(gatewayIp, unEncodeUrl);
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var list = convertString2Json(resp.body)['data'] as List<dynamic>;
    List<VideoVo> result = [];
    for (var item in list) {
      result.add(VideoVo.fromJson(item));
    }
    return result;
  } else {
    return null;
  }
}

// 获得我的所有收藏夹
Future<List<String>> getMyCategoryListRequest(BuildContext context, int id, String token) async {
  Uri url = Uri.http(gatewayIp, '$getMyCategoryList$id');
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    List<dynamic> res = convertString2Json(resp.body)['data'] as List<dynamic>;
    return List<String>.from(res);
  } else {
    return null;
  }
}

// 获取收藏夹具体内容
Future<List<VideoVo>> getMyCollectByNameRequest(BuildContext context, int id, String token, String collectorName, {int page = 1}) async {
  Uri url = Uri.http(gatewayIp, '$getMyCollectByName$id/$collectorName/$page');
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var list = convertString2Json(resp.body)['data'] as List<dynamic>;
    List<VideoVo> result = [];
    for (var item in list) {
      result.add(VideoVo.fromJson(item));
    }
    return result;
  } else {
    return null;
  }
}

// 更新收藏夹名称
Future<String> updateCollectorNameRequest(BuildContext context, int id, String token, String oldCollectName, String newCollectorName) async {
  Uri url = Uri.http(gatewayIp, '$updateCollectorName$id');
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.put(url, headers: header,
      body: jsonEncode({
        "category": oldCollectName,
        "newCategory": newCollectorName
      })
  );
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 删除收藏夹
Future<String> deleteCollectorRequest(BuildContext context, int id, String token, String collectorName) async {
  Uri url = Uri.http(gatewayIp, '$deleteCollector$id/$collectorName');
  var resp = await http.delete(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 删除收藏视频
Future<String> deleteCollectorItemRequest(BuildContext context, int id, String token, String collectorName, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$deleteCollectorItem$id');
  var header = {
    "token" : token
  };
  header.addEntries(jsonFormat.entries);
  var resp = await http.delete(url, headers: header ,
    body: jsonEncode({
        "videoId": videoId,
        "category": collectorName
    })
  );
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 添加收藏视频
Future<String> addNewCollectItem(BuildContext context, int id, String token, String collectorName, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$addCollectItem$id');
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers: header,
    body: jsonEncode({
      "videoId": videoId,
      "category": collectorName
    })
  );
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 删除视频
Future<String> deleteMyVideoRequest(BuildContext context, String token, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$deleteMyVideo$videoId');
  var resp = await http.delete(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    print('删除视频失败 ${convertString2Json(resp.body)}');
    return null;
  }
}

// 更新视频信息
Future<String> updateVideoInfoRequest(BuildContext context, String token, String videoId, int userId, { String videoName = "", String description = "", String profile = null, bool isPublished = true, String tags = ""}) async {
  Uri url = Uri.http(gatewayIp, updateVideoInfo);
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.put(url, headers: header,
    body: jsonEncode({
      "videoId": videoId,
      "videoName": videoName,
      "profile": profile,
      "isPublished": isPublished,
      "tags": tags,
      "authorId": userId,
      "description": description
    })
  );
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 点赞点踩视频
Future<String> likeVideoRequest(BuildContext context, int userId, String token, String videoId, VideoPreference preference) async {
  Uri url = generateLikePath(preference, userId);
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers: header,
    body: videoId,
  );
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

Uri generateLikePath(VideoPreference preference, int userId) {
  String path;
  switch (preference) {
    case VideoPreference.LIKE:
      path = likeVideo;
      break;
    case VideoPreference.DISLIKE:
      path = dislikeVideo;
      break;
  }
  Uri url = Uri.http(gatewayIp, '$path$userId');
  return url;
}

// 取消点赞点踩
Future<String> cancelLikeVideoRequest(BuildContext context, int userId, String token, String videoId, VideoPreference preference) async {
  Uri url = generateLikePath(preference, userId);
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.delete(url, headers: header,
      body: videoId
  );
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 获取是否点赞视频
Future<bool> getIsLikeVideoRequest(BuildContext context, int userId, String token, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$getIsLikeUrl$userId/$videoId');
  var header = {"token": token};
  var resp = await http.get(url, headers: header);
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['data'];
  } else {
    return null;
  }
}

// 获取是否点踩视频
Future<bool> getIsDislikeVideoRequest(BuildContext context, int userId, String token, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$getIsDislikeVideo$userId/$videoId');
  var header = {"token": token};
  var resp = await http.get(url, headers: header);
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['data'];
  } else {
    return null;
  }
}

// 是否收藏视频
Future<bool> getIsCollectVideoRequest(BuildContext context, int userId, String token, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$getIsCollectVideo$userId/$videoId');
  var header = {"token": token};
  var resp = await http.get(url, headers: header);
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['data'];
  } else {
    return null;
  }
}

Future<Video> getVideoDetailRequest(BuildContext context, String token, String videoId) async {
  Uri url = Uri.http(gatewayIp, '$getVideoDetail$videoId');
  var header = {"token": token};
  var resp = await http.get(url, headers: header);
  if (handleFailure(context, resp)) {
    return Video.fromJson(convertString2Json(resp.body)['data']);
  } else {
    return null;
  }
}

Future<List<Video>> getRandomVideosRequest(BuildContext context, String token, int id) async {
  var uri = Uri.http(gatewayIp, getRandomVideo);
  var resp = await http.get(uri, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    List<Video> result = [];
    var list = convertString2Json(resp.body)['data'] as List<dynamic>;
    for (var item in list) {
      result.add(Video.fromJson(item));
    }
    return result;
  } else {
    return null;
  }
}

// 搜索视频
Future<List<Video>> searchVideoRequest(BuildContext context, String words) async {
  Uri url = Uri.http(gatewayIp, '$searchVideo$words');
  var resp = await http.get(url);
  if (handleFailure(context, resp)) {
    List<Video> result = [];
    var list = convertString2Json(resp.body)['data'] as List<dynamic>;
    for (var item in list) {
      result.add(Video.fromJson(item));
    }
    return result;
  } else {
    return null;
  }
}

// 获取评论树
Future<CommentTree> getVideoCommentTreeRequest(BuildContext context, 
    String videoId, {int page = 1}) async {
  Uri url = Uri.http(gatewayIp, '$getVideoCommentTree$videoId/$page');
  var resp = await http.get(url);
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body)['data'];
    return CommentTree.fromJson(data);
  } else {
    return null;
  }
}

// 获取评论树子树
Future<List<CommentTreeNode>> getVideoCommentTreeChildRequest(
  BuildContext context, String videoId, int commentId, {int page = 1}) async {
  Uri url = Uri.http(gatewayIp, '$getVideoCommentTreeChild$videoId/$commentId/$page');
  var resp = await http.get(url);
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body)['data'];
    return convertJson2Node(data);
  } else {
    return null;
  }
}

// 发表评论
Future<CommentTreeNode> publishCommentRequest(BuildContext context, int id, String token,
  String videoId, String content, {int parentId = 0, int replyId = null}) async {
  Uri url = Uri.http(gatewayIp, '$publishComment$id');
  var header = {"token": token};
  header.addEntries(jsonFormat.entries);
  var resp = await http.post(url, headers:  header,
    body: jsonEncode({
      "videoId": videoId,
      "authorId": id,
      "content": content,
      "parentId": parentId,
      "replyId": replyId
    })
  );
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body)['data'] as Map<String, dynamic>;
    return CommentTreeNode.fromJson(data);
  } else {
    return null;
  }
}


enum CommentOpsType {
  CANCEL,
  SET,
}
enum CommentPreference {
  LIKE,
  DISLIKE
}
// 评论操作
Future<String> commentOperateRequest(BuildContext context, int id, String token,
  String videoId, int commentId, {CommentOpsType ops = CommentOpsType.SET,
      CommentPreference preference = CommentPreference.LIKE}) async {
  Uri url;
  switch (preference) {
    case CommentPreference.LIKE:
      url = Uri.http(gatewayIp, '$likeComment$id/$videoId/$commentId');
      break;
    case CommentPreference.DISLIKE:
      url = Uri.http(gatewayIp, '$dislikeComment$id/$videoId/$commentId');
      break;
  }
  var resp;
  switch (ops) {
    case CommentOpsType.SET:
      resp = await http.post(url, headers: {
        "token": token
      });
      break;
    case CommentOpsType.CANCEL:
      resp = await http.delete(url, headers: {
        "token": token
      });
  }
  if (handleFailure(context, resp)) {
    return convertString2Json(resp.body)['message'];
  } else {
    return null;
  }
}

// 获取用户信息
Future<Map<int, UserVo>> getUserProfileRangeRequest(BuildContext context, List<int> userIds, String token) async {
  Uri url = Uri.http(gatewayIp, getUserProfileRange, {
    "ids": userIds.map((e) => e.toString())
  });
  var resp = await http.get(url, headers: {
    "token": token
  });
  if (handleFailure(context, resp)) {
    var data = convertString2Json(resp.body)['data'] as List<dynamic>;
    List<UserVo> result = [];
    for (var value in data) {
      result.add(UserVo.fromJson(value));
    }
    return result.fold(<int, UserVo>{}, (previousValue, element) async {
      var value = await previousValue;
      value.addAll({element.id: element});
      return value;
    });
  } else {
    return null;
  }
}

// 获取一批评论是否点赞或者是点踩
Future<List<bool>> getCommentIsLikeRangeRequest(BuildContext context, List<int> commentIds, String token,
    String videoId, int userId, {CommentPreference type = CommentPreference.LIKE}) async {
  Uri url;
  if (type == CommentPreference.LIKE) {
    url = Uri.http(gatewayIp, '$getCommentIsLikeRange$userId/$videoId', {
      "ids": commentIds.map((e) => e.toString())
    });
  } else {
    url = Uri.http(gatewayIp, '$getCommentIsDislikeRange$userId/$videoId', {
      "ids": commentIds.map((e) => e.toString())
    });
  }

  var resp = await http.get(url, headers:  {
    "token": token
  });
  if (handleFailure(context, resp)) {
    List<bool> result = [];
    var data = convertString2Json(resp.body)['data'] as List<dynamic>;
    for (var value in data) {
      result.add(value);
    }
    return result;
  } else {
    return null;
  }
}